﻿using Dm;
using GameForm.Gamelib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GameForm
{
    public partial class MainForm : Form
    {
        private Dm.dmsoft dm = null;

        public MainForm()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

        private TaskRunInfo runInfo = new TaskRunInfo();

        private void button1_Click(object sender, EventArgs e)
        {
            
            if (button1.Text == "开始执行")
            {
                runInfo.IsRun = true;
                _ = Task.Run(async () =>
                {
                    try
                    {
                        var gameApi = new GameTaskApi(dm, cbx_Task.SelectedItem as string);
                        var task = gameApi.GetTask();
                        if (task == null)
                        {
                            MessageBox.Show("暂时不支持的任务");
                        }
                        else
                        {
                            await task.RunTask(runInfo);
                        }
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    
                });
                button1.Text = "停止执行";
            }
            else
            {
                runInfo.IsRun = false;
                button1.Text = "开始执行";
            }
   
            
        }

        /// <summary>
        /// 查询并绑定窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            dm = new Dm.dmsoft();
            dm.SetPath(AppDomain.CurrentDomain.BaseDirectory+"images");
            var hwnd = dm.FindWindow("", "梦幻西游：时空");
            lab_GameInfo.Text = "";
            if(hwnd==0)
            {
                MessageBox.Show("未找到游戏窗口");
                return;
            }
            dm.BindWindow(hwnd, "gdi", "windows", "windows", 0);
            lab_GameInfo.Text = $"游戏窗口：{hwnd}";
        }
    }
}
